package com.ruoyi.web.controller.edh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CouponException;
import com.ruoyi.common.exception.ShippingTemplateException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edh.domain.EdhMasterOrder;
import com.ruoyi.edh.domain.dto.EdhOrderGoodsDto;
import com.ruoyi.edh.service.IEdhMasterOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 易订货-主订单Controller
 *
 * @author xxx
 * @date 2020-09-02
 */
@RestController
@RequestMapping("/edh/masterorder")
@Api(description = "易订货-主订单")
public class EdhMasterOrderController extends BaseController {
    @Autowired
    private IEdhMasterOrderService edhMasterOrderService;

    /**
     * 查询易订货-主订单列表
     */
    @GetMapping("/list")
    @ApiOperation("易订货-主订单列表")
    public TableDataInfo list(EdhMasterOrder edhMasterOrder) {
        startPage();
        List<EdhMasterOrder> list = edhMasterOrderService.selectEdhMasterOrderList(edhMasterOrder);
        return getDataTable(list);
    }

    /**
     * 导出易订货-主订单列表
     */
    @Log(title = "易订货-主订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出易订货-主订单列表")
    public AjaxResult export(EdhMasterOrder edhMasterOrder) {
        List<EdhMasterOrder> list = edhMasterOrderService.selectEdhMasterOrderList(edhMasterOrder);
        ExcelUtil<EdhMasterOrder> util = new ExcelUtil<EdhMasterOrder>(EdhMasterOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 获取易订货-主订单详细信息
     */
    @GetMapping
    @ApiOperation("获取易订货-主订单详细信息")
    public AjaxResult getInfo(Long id) {
        return AjaxResult.success(edhMasterOrderService.selectEdhMasterOrderById(id));
    }

    /**
     * 新增易订货-主订单
     */
    @Log(title = "易订货-主订单", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增易订货-主订单")
    public AjaxResult add(@RequestBody EdhMasterOrder edhMasterOrder) {
        return toAjax(edhMasterOrderService.insertEdhMasterOrder(edhMasterOrder));
    }

    /**
     * 修改易订货-主订单
     */
    @Log(title = "易订货-主订单", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改易订货-主订单")
    public AjaxResult edit(@RequestBody EdhMasterOrder edhMasterOrder) {
        return toAjax(edhMasterOrderService.updateEdhMasterOrder(edhMasterOrder));
    }

    /**
     * 删除易订货-主订单
     */
    @Log(title = "易订货-主订单", businessType = BusinessType.DELETE)
    @GetMapping("/delete")
    @ApiOperation("删除易订货-主订单")
    public AjaxResult remove(String ids) {
        return toAjax(edhMasterOrderService.deleteEdhMasterOrderByIds(ids.split(",")));
    }

    @PostMapping("/addShippingOrder")
    @Log(title = "易订购-订单新增", businessType = BusinessType.INSERT)
    @ApiOperation("易订购-添加购物订单")
    public AjaxResult addShippingOrder(@RequestBody EdhOrderGoodsDto edhOrderGoodsDto) {
        try {
            EdhMasterOrder o = edhMasterOrderService.addShippingOrder(edhOrderGoodsDto);
            return AjaxResult.success(o);
        } catch (CouponException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        } catch (ShippingTemplateException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }
}
